package com.lms.vo;

import com.lms.bean.Book;
import com.lms.bean.Request;
import com.lms.bean.User;
import org.springframework.beans.BeanUtils;

public class RequestVO extends Request {
    private String username;
    private String bookName;
    private Long bookId;
    private Long userId;

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public static RequestVO convert(Request request) {

        parse(request);
        RequestVO requestVO = new RequestVO();
        BeanUtils.copyProperties(request, requestVO);
        User user = request.getUser();
        Book book = request.getBook();
        requestVO.setUser(null);
        requestVO.setBook(null);

        if(null!=user){
            requestVO.setUsername(user.getUsername());
            requestVO.setUserId(user.getUserId());
        }

        if(null!=book){
            requestVO.setBookName(book.getBookName());
            requestVO.setBookId(book.getBookId());
        }

        return requestVO;
    }

    public static void parse(Request request) {
        Long actionCode = request.getActionCode();
        if(actionCode == 0L){
            request.setActionString("借书");
        }else{
            request.setActionString("还书");
        }
        Long handleCode = request.getHandleCode();
        // 0L == 未处理   1L == 借书成功   2L == 借书请求被拒绝   3L ==  还书成功    4L == 还书失败
        if(handleCode == 0L){
            request.setHandleString("未处理");
        }else if(handleCode == 1L){
            request.setHandleString("借书成功");
        }else if(handleCode == 2L){
            request.setHandleString("借书请求被拒绝");
        }else if(handleCode == 3L){
            request.setHandleString("还书成功");
        }else if(handleCode == 4L){
            request.setHandleString("还书失败");
        }
    }
}
